import Game from "../base/Game";
import BookEvent_Emit from "../event/BookEvent_Emit";
import GameEvent from "../event/GameEvent";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Player extends cc.Component {

    _armatureDisPlay: dragonBones.ArmatureDisplay;
    _armature: any;

    // LIFE-CYCLE CALLBACKS:

    onLoad ()
    {
        //获取 ArmatureDisplay
        this._armatureDisPlay = this.getComponent(dragonBones.ArmatureDisplay)
        //获取 Armatrue
        this._armature = this._armatureDisPlay.armature()
        //添加动画监听
        this._armatureDisPlay.addEventListener("loopComplete",this.onLoopComplete, this);
        this._armatureDisPlay.addEventListener("frameEvent",this.onFrameEvent, this);
        this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_OUT_COMPLETE, this.animationEventHandler, this)
    }

    start () {

    }

    private onLoopComplete(e):void
    {
      // if(this.mState == UnitState.ATTACK) 
      // {
      // 	this.isPlayingAttack = false;
      // 	this.dispatchEventWith(BookEvent.UNIT_PLAY_ATTACK_END);
      // }
      // else if(UnitState.SPECIAL == this.mState)
      // {
      // 	//播放完出现之后播放待机
      // 	this.idle();
      // }
      // else if(UnitState.IDLE == this.mState)
      // {
      // 	if(this.needDispatchStandEnd) this.dispatchEventWith(BookEvent.UNIT_PLAY_STAND_END);
      // }
      // else if(UnitState.DIE == this.mState)
      // {
      // 	this.dispatchEventWith(BookEvent.UNIT_PLAY_DIE_END);
      // }
      }
    
    private onFrameEvent(e):void
	{
		//console.warn("e2",e)
		if(e.name == "playRelease")
		{
			//this.dispatchEventWith(BookEvent.UNIT_RELEASE_EFFECT_EVENT);
		}
		else if(e.name == "playBullet")
		{
        //this.dispatchEventWith(BookEvent.UNIT_PLAY_BULLET_EVENT);
        GameEvent.ins.sendEvent(BookEvent_Emit.PLAY_BULLET_EVENT,this.node);
		}
	}

    private animationEventHandler(event)
    {
        if (event.type == dragonBones.EventObject.FADE_IN_COMPLETE) {
          cc.warn(event.detail.animationName + ' fade in complete');
        } else if (event.type == dragonBones.EventObject.FADE_OUT_COMPLETE) {
          cc.warn(event.detail.animationName + ' fade out complete');
        }
    }

    // update (dt) {}
}
